<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">楼宇名称：</div>
      <el-input v-model="params.name" placeholder="请输入内容" class="search-main" />
      <el-button type="primary" @click="searchBuilding">查询</el-button>

    </div>
    <!-- 新增弹框按钮 -->
    <!-- 添加楼宇弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="580px"
      @close="closeDialog"
    >
      <template>
        <el-form ref="ruleForm" class="demo-ruleForm" :model="fromBuilding" :rules="rules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="fromBuilding.name" placeholder="请输入楼宇名称" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model.number="fromBuilding.floors" placeholder="请输入楼宇层数" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model.number="fromBuilding.area" placeholder="请输入再管面积">
              <template slot="append">m²</template>
            </el-input>
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model.number="fromBuilding.propertyFeePrice" placeholder="请输入物业费">
              <template slot="append">元/m²</template>
            </el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>
      </template>
    </el-dialog>

    <el-button type="primary" @click="addBuilding">添加楼宇</el-button>
    <el-button @click="exportToExcel">导出Ex</el-button>
    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="list"
      >
        <el-table-column
          type="index"
          label="序号"
        />
        <el-table-column
          label="楼宇名称"
          width="180"
          prop="name"
        />
        <el-table-column
          label="层数"
          prop="floors"
        />
        <el-table-column
          label="在管面积(m²)"
          width="120"
          prop="area"
        />
        <el-table-column
          label="物业费(元/m²)"
          width="120"
          prop="propertyFeePrice"
        />
        <el-table-column
          label="状态"
          prop="status"
        >
          <template #default="scope">{{ nowState(scope.row.status) }}</template>
        </el-table-column>
        <el-table-column
          label="操作"
        >
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              @click="compile(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="removeID(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-container">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :current-page="1"
          :page-sizes="[10,20,50,100]"
          :page-size="10"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { buildingGet, buildingAdd, buildingXiang, buildingPut, delBuildingListAPI } from '@/api/building'
import { utils, writeFileXLSX } from 'xlsx'
export default {
  name: 'Building',
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
        name: ''
      },
      list: [],
      total: 0,
      dialogVisible: false,
      fromBuilding: {
        name: '', // 楼宇名称
        floors: null, // 楼宇层数
        area: null, // 再管面积
        propertyFeePrice: null // 物业费
      },
      rules: {
        name: [
          { required: true, message: '请输入楼宇名称', trigger: 'blur' }
        ],
        floors: [
          { required: true, message: '请输入楼宇层数', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '请输入再管面积', trigger: 'blur' }
        ],
        propertyFeePrice: [
          { required: true, message: '请输入物业费', trigger: 'blur' }
        ]
      },
      title: ''
    }
  },
  mounted() {
    this.getBuilding()
  },
  methods: {
    // 获取数据,渲染列表
    async getBuilding() {
      const res = await buildingGet(this.params)
      console.log(res)
      this.total = res.data.total
      this.list = res.data.rows
      console.log(this.list)
    },
    searchBuilding() {
      this.getBuilding()
    },
    // 计算状态
    nowState(id) {
      // console.log(id)
      if (id === 0) {
        return '租赁中'
      } else {
        return '闲置中'
      }
    },
    // 页面跳转
    handleCurrentChange(val) {
      console.log(val)
      this.params.page = val
      this.getBuilding()
    },
    // 页面大小
    handleSizeChange(val) {
      // console.log(val)
      this.params.pageSize = val
      this.params.page = 1
      this.getBuilding()
    },
    // 打开弹窗
    addBuilding() {
      this.dialogVisible = true
      this.title = '添加楼宇'
      delete this.fromBuilding.id
    },
    // 关闭弹窗
    closeDialog() {
      this.$refs.ruleForm.resetFields()
      this.getBuilding()
      // this.dialogVisible = false
    },
    // 发送添加请求
    addlouyu() {
      this.dialogVisible = false
    },
    // 提交整体验证
    confirmAdd() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (this.fromBuilding.id) {
          // console.log(111)
          await buildingPut(this.fromBuilding)
          this.dialogVisible = false
        } else {
          if (valid) {
            try {
              await buildingAdd(this.fromBuilding)
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.dialogVisible = false
            } catch (error) {
              this.$message({
                message: '操作失败',
                type: 'error'
              })
            }
          }
        }
      })
    },
    async compile(val) {
      console.log(val)
      this.title = '编辑楼宇'
      this.dialogVisible = true
      const res = await buildingXiang(val.id)
      console.log(res)
      const { name, floors, area, propertyFeePrice, id } = res.data
      this.fromBuilding = { name, floors, area, propertyFeePrice, id }
      // console.log(this.fromBuilding)
    },
    // 删除功能
    removeID(id) {
      // console.log(row)
      delBuildingListAPI(id)
      this.getBuilding()
    },
    async exportToExcel() {
      const res = await buildingGet(this.params)
      const tableHeader = ['name', 'floors', 'area', 'propertyFeePrice', 'status']
      // 处理数据保证
      const sheetData = res.data.rows.map((item) => {
        const obj = {}
        tableHeader.forEach(key => {
          obj[key] = item[key]
        })
        return obj
      })
      // 创建一个工作表
      const worksheet = utils.json_to_sheet(sheetData)
      // 创建一个新的工作簿
      const workbook = utils.book_new()
      // 把工作表添加到工作簿
      utils.book_append_sheet(workbook, worksheet, 'Data')
      // 改写表头
      utils.sheet_add_aoa(worksheet, [['楼宇名称', '层数', '在管面积(㎡)', '物业费(㎡)', '状态']], { origin: 'A1' })
      writeFileXLSX(workbook, 'SheetJSVueAoO.xlsx')
    }

  }
}
</script>

<style lang="scss" scoped>
.department-container{
  padding:10px;
}
.search-container{
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237,237,237,.9);;
  padding-bottom: 20px;
  .search-label{
    width:100px;
  }
  .search-main{
    width: 220px;
    margin-right: 10px;
  }
}
  .page-container{
    padding:4px 0px;
    text-align: right;
  }
  .building-container{
    overflow: auto;
    height: calc(100vh - 100px);
  }
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

</style>
